package com.zzw.config;

import com.zzw.bean.Msg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    
    /**
     * 处理运行时异常
     * @param e 运行时异常
     * @return 包含错误信息的Msg对象
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Msg handleRuntimeException(RuntimeException e) {
        log.error("运行时异常：", e);
        return Msg.fail(e.getMessage());
    }
    
    /**
     * 处理所有其他未捕获的异常
     * @param e 异常
     * @return 包含错误信息的Msg对象
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Msg handleException(Exception e) {
        log.error("系统异常：", e);
        return Msg.fail("系统异常：" + e.getMessage());
    }
}
